package org.glut.AppStoreProject.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.glut.AppStoreProject.entity.AppList;
import org.glut.AppStoreProject.service.AppListImpl;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(value = "/appslist")

public class MyAppListServlet extends HttpServlet {
    static Logger log = Logger.getLogger(MyAppListServlet.class);

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws  IOException {
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        AppList condition = new AppList();
        try{
            List<AppList> appLists = new AppListImpl().getAppList(condition);
            Gson gson = new GsonBuilder()
                    .setDateFormat("yyyy-MM-dd HH:mm:ss")
                    .setPrettyPrinting() //设置json 有缩进格式
                    .create();
            String json = gson.toJson(appLists);
            //将json 字符串写回前端
            writer.write(json);
        } catch (Exception e) {
            log.error("ERROR: ",e);
        }

    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws jakarta.servlet.ServletException, IOException {
        doGet(req, resp);
    }
}
